﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
using ForkCarRelationManager.Utility;
using ForkCarRelationManager.Business;
namespace ForkCarRelationManager
{
    public partial class MaintenInforForm : Form
    {
        Util util = new Util();
        MaintenBI maintenBI = new MaintenBI();
        private int maintenid=0;
        public MaintenInforForm()
        {
            InitializeComponent();
        }
        public  MaintenInforForm(int maintenid)
        {
            this.maintenid = maintenid;
            InitializeComponent();
        }
        private void MaintenInforForm_Load(object sender, EventArgs e)
        {
            initForm();
        }
        private void initForm()
        {
            initText();
            initPart();
        }
        private void initText()
        {
            if (maintenid != 0)
            {
                DataTable textTable = maintenBI.GetmaintenDetailsbyid(maintenid);
                TBcontact.Text = textTable.Rows[0]["ContactName"].ToString();
                TBclient.Text = textTable.Rows[0]["ClientName"].ToString();
                TBdevicenum.Text = textTable.Rows[0]["DeviceNum"].ToString();
                if (textTable.Rows[0]["Ifmaintain"].ToString() == "1")
                {
                    maintainradio.Checked = true;
                }
                else
                    maintenradio.Checked = true;
                if (textTable.Rows[0]["Iffree"].ToString() == "0")
                {
                    radioButton3.Checked = true;
                }
                else if (textTable.Rows[0]["Iffree"].ToString() == "1")
                    radioButton4.Checked = true;
                else
                    radioButton5.Checked = true;
                TBprovicenum.Text = textTable.Rows[0]["Provicenum"].ToString();
                TBphone.Text = textTable.Rows[0]["ContactPhone"].ToString();
                TBSerialNum.Text = textTable.Rows[0]["SerialNum"].ToString();
                TBbrand.Text = textTable.Rows[0]["Brand"].ToString();
                TBcartype.Text = textTable.Rows[0]["CarType"].ToString();
                TBModel.Text = textTable.Rows[0]["Model"].ToString();
                TBsumhour.Text = textTable.Rows[0]["SumHour"].ToString();
                TBmaintor.Text = textTable.Rows[0]["MaintorName"].ToString();
                DDworkhour.Value = Convert.ToInt32(textTable.Rows[0]["WorkTime"]);
                TBdata.Text = textTable.Rows[0]["MaintenDate"].ToString().Replace("0:00:00","");
                Symptomtext.Text = textTable.Rows[0]["Symptom"].ToString();
                maintentext.Text = textTable.Rows[0]["MaintenContent"].ToString();
                ClientAdvicetext.Text = textTable.Rows[0]["ClientAdvice"].ToString();
                BackMessagetext.Text = textTable.Rows[0]["BackMessage"].ToString();
                CheckContenttext.Text = textTable.Rows[0]["CheckContent"].ToString();
                Changeparttext.Text = textTable.Rows[0]["Changepart"].ToString();
            }
        }
        private void initPart()
        {
            if (maintenid != 0)
            {
                DataTable partsTable = maintenBI.GetpartDetailsbyid(maintenid);
                dataGridViewX1.DataSource = partsTable;
                double sumprice = 0;

                if (dataGridViewX1.Rows.Count != 0)
                {
                    for (int rowindex = 0; rowindex < dataGridViewX1.Rows.Count; rowindex++)
                    {

                        if (dataGridViewX1.Rows[rowindex].Cells[6].Value != null)
                        {
                            double price = 0.0;
                            if (Regex.IsMatch(dataGridViewX1.Rows[rowindex].Cells[6].Value.ToString(), @"^[+-]?\d*[.]?\d*$"))
                            {
                                price = Convert.ToDouble(dataGridViewX1.Rows[rowindex].Cells[6].Value.ToString());
                            }
                            sumprice += price;
                        }
                    }
                }
                pricetext.Text = sumprice.ToString("f2");
            }
        }
        private void dataGridViewX1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            double sumprice = 0;
            if (dataGridViewX1.Rows[e.RowIndex].Cells[3].Value != null && dataGridViewX1.Rows[e.RowIndex].Cells[5].Value != null&&dataGridViewX1.Rows[e.RowIndex].Cells[3].Value.ToString().Trim()!="")
            {
                string partCount = dataGridViewX1.Rows[e.RowIndex].Cells[3].Value.ToString().Trim();
                string UnitPrice="";
                if (dataGridViewX1.Rows[e.RowIndex].Cells[5].Value.ToString().Trim() != "")
                {
                    UnitPrice = Convert.ToDouble(dataGridViewX1.Rows[e.RowIndex].Cells[5].Value.ToString().Trim()).ToString("f2");
                }
                dataGridViewX1.Rows[e.RowIndex].Cells[5].Value = UnitPrice;
                if (util.isnumeric(partCount) && (util.isnumeric(UnitPrice) || Regex.IsMatch(UnitPrice, @"^[+-]?\d*[.]?\d*$")))
                {

                    double Price = Convert.ToInt32(partCount) * Convert.ToDouble(UnitPrice);

                    dataGridViewX1.Rows[e.RowIndex].Cells[6].Value = Price.ToString("f2");
                }
            }
            if (dataGridViewX1.Rows.Count != 0)
            {
                for (int rowindex = 0; rowindex < dataGridViewX1.Rows.Count; rowindex++)
                {

                    if (dataGridViewX1.Rows[rowindex].Cells[6].Value != null)
                    {
                        double price = 0.0;
                        if (Regex.IsMatch(dataGridViewX1.Rows[rowindex].Cells[6].Value.ToString(), @"^[+-]?\d*[.]?\d*$"))
                        {
                            price = Convert.ToDouble(dataGridViewX1.Rows[rowindex].Cells[6].Value.ToString());
                        }
                        sumprice += price;
                    }
                }
            }
            pricetext.Text = sumprice.ToString("f2");
        }

        private void Btn_addmainten_Click(object sender, EventArgs e)
        {
            bool ifsubmittext = true;
            int maintain;//必要
            int free;//必要
            int hour = Convert.ToInt32(DDworkhour.Value);//必要

            bool ifmaintanin = maintainradio.Checked;
            
            if (ifmaintanin)
            {
                maintain = 1;
            }
            else
                maintain = 0;


            if (radioButton3.Checked)
                free = 0;
            else if (radioButton4.Checked)
                free = 1;
            else
                free = 2;
            string Provicenum = TBprovicenum.Text.Trim();//充分不必要
            string symptom = Symptomtext.Text.Trim();//必要
            string maintenContent = maintentext.Text.Trim();//必要
            string clientAdvice = ClientAdvicetext.Text.Trim();//充分不必要
            string backMessage = BackMessagetext.Text.Trim();//充分不必要
            string checkcontext = CheckContenttext.Text.Trim();
            string changepart = Changeparttext.Text.Trim();
            if (( symptom == "" || maintenContent == "") && ifsubmittext)
            {
                ifsubmittext = false;
                MessageBox.Show("小时数,故障内容,维修内容不能为空");
            }
            //零件部分

            int rowcount = dataGridViewX1.RowCount;
            int colmncount = 8;//列数
            List<int> rowexist = new List<int>();
            bool ifsubmitpart = true;
            bool ifpartstand = true;//是否需要提交零件表

            for (int i = 0; i < rowcount; i++)
            {
                for (int j = 1; j < colmncount; j++)
                {
                    if (dataGridViewX1.Rows[i].Cells[j].Value != null)
                    {
                        if (dataGridViewX1.Rows[i].Cells[j].Value.ToString().Trim() != "" || dataGridViewX1.Rows[i].Cells[j].Value != null)
                        {
                            rowexist.Add(i);
                            break;
                        }
                    }
                }
            }
            if (rowexist.Count != 0)
            {
                ifsubmitpart = true;
            }
            else
            {

                ifsubmitpart = false;

            }
            //判断所填内容是否规范
            if (ifsubmitpart && ifsubmittext)
            {
                for (int i = 0; i < rowexist.Count; i++)
                {
                    int rowindex = rowexist[i];

                    string PartName = "";
                    string Standard = "";
                    string partCount = "";

                    string Unit = "";
                    string UnitPrice = "";
                    string Price = "";
                    string Remark = "";
                    if (dataGridViewX1.Rows[rowindex].Cells[1].Value != null)
                    {
                        PartName = dataGridViewX1.Rows[rowindex].Cells[1].Value.ToString();
                    }
                    if (dataGridViewX1.Rows[rowindex].Cells[2].Value != null)
                    {
                        Standard = dataGridViewX1.Rows[rowindex].Cells[2].Value.ToString();
                    }
                    if (dataGridViewX1.Rows[rowindex].Cells[3].Value != null)
                    {
                        partCount = dataGridViewX1.Rows[rowindex].Cells[3].Value.ToString();//数字
                    }
                    if (dataGridViewX1.Rows[rowindex].Cells[4].Value != null)
                    {
                        Unit = dataGridViewX1.Rows[rowindex].Cells[4].Value.ToString();
                    }
                    if (dataGridViewX1.Rows[rowindex].Cells[5].Value != null)
                    {
                        UnitPrice = dataGridViewX1.Rows[rowindex].Cells[5].Value.ToString();//数字
                    }
                    if (dataGridViewX1.Rows[rowindex].Cells[6].Value != null)
                    {
                        Price = dataGridViewX1.Rows[rowindex].Cells[6].Value.ToString();//数字
                    }
                    if (dataGridViewX1.Rows[rowindex].Cells[7].Value != null)
                    {
                        Remark = dataGridViewX1.Rows[rowindex].Cells[7].Value.ToString();//只有备注能为空;
                    }

                    if (!util.isnumeric(partCount) || !Regex.IsMatch(UnitPrice, @"^[+-]?\d*[.]?\d*$") || !Regex.IsMatch(Price, @"^[+-]?\d*[.]?\d*$") || PartName.Trim() == "" )
                    {
                        MessageBox.Show("名称不能为空，且数量,单价必须为数字");
                        ifpartstand = false;
                        break;
                    }

                }
            }
            if (ifsubmittext)//提交维修信息
            {

                if (maintenBI.updatemaintenRecordbyid(maintenid, maintain, free,Provicenum, symptom, maintenContent, clientAdvice, backMessage, hour, checkcontext, changepart))
                {

                    if (ifsubmitpart)//提交所用零件信息
                    {
                        bool finish = true;
                        if (ifpartstand)
                        {
                            for (int i = 0; i < rowexist.Count; i++)
                            {
                                int rowindex = rowexist[i];
                                string PartName = dataGridViewX1.Rows[rowindex].Cells[1].Value.ToString();
                                string Standard = "";
                                if (dataGridViewX1.Rows[rowindex].Cells[2].Value != null)
                                {
                                    Standard = dataGridViewX1.Rows[rowindex].Cells[2].Value.ToString();//规格可以为空
                                }
                                int partCount=0 ;
                                if (dataGridViewX1.Rows[rowindex].Cells[3].Value.ToString().Trim() !="")
                                {
                                    partCount = Convert.ToInt32(dataGridViewX1.Rows[rowindex].Cells[3].Value);//数字
                                }
                                string Unit = "";
                                if (dataGridViewX1.Rows[rowindex].Cells[4].Value != null)
                                {
                                    Unit = dataGridViewX1.Rows[rowindex].Cells[4].Value.ToString();//单位可以为空
                                }
                                double UnitPrice = 0;
                                if (dataGridViewX1.Rows[rowindex].Cells[5].Value.ToString().Trim() != "")
                                {
                                   UnitPrice= Convert.ToDouble(dataGridViewX1.Rows[rowindex].Cells[5].Value);//数字
                                }
                                double Price = Convert.ToDouble(dataGridViewX1.Rows[rowindex].Cells[6].Value);//数字
                                string Remark = "";
                                if (dataGridViewX1.Rows[rowindex].Cells[7].Value != null)
                                {
                                    Remark = dataGridViewX1.Rows[rowindex].Cells[7].Value.ToString();//备注能为空
                                }
                                int partID = Convert.ToInt32(dataGridViewX1.Rows[rowindex].Cells[8].Value);
                                bool ifok = maintenBI.updatepartbyid(partID, PartName, Standard, partCount, Unit, UnitPrice, Price, Remark);
                                if (!ifok)
                                {
                                    finish = false;
                                }

                            }
                            if (finish)
                            {
                                maintenBI.updatefearByid(maintenid, Convert.ToDouble(pricetext.Text));
                                maintenBI.updateworkload(TBmaintor.Text.Trim(), Convert.ToDouble(pricetext.Text), Convert.ToInt32(DDworkhour.Value), maintenid);
                                MessageBox.Show("维修信息更改成功");
                                this.DialogResult = DialogResult.Yes;
                            }
                            else
                                MessageBox.Show("维修信息更改失败");
                        }


                    }
                    else
                    {
                        maintenBI.updatefearByid(maintenid, Convert.ToDouble(pricetext.Text));
                        maintenBI.updateworkload(TBmaintor.Text.Trim(), Convert.ToDouble(pricetext.Text) ,Convert.ToInt32( DDworkhour.Value), maintenid);
                        MessageBox.Show("维修信息更改成功");
                        this.DialogResult = DialogResult.Yes;
                    }


                }
                else
                    MessageBox.Show("维修信息更改失败");


            }
        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewX1.CurrentRow != null)
            {
                string id = dataGridViewX1.CurrentRow.Cells[8].Value.ToString();
                if (dataGridViewX1.Columns[e.ColumnIndex].Name == "Column8")
                {
                    maintenBI.deletepartbyid(Convert.ToInt32(id));
                    initPart();
                }
            }
        }

        private void MaintenInforForm_FormClosing(object sender, FormClosingEventArgs e)
        {
           
        }

        private void textBoxX10_TextChanged(object sender, EventArgs e)
        {

        }

        private void addfear_Click(object sender, EventArgs e)
        {
            addfearForm addfearform = new addfearForm(maintenid);
            addfearform.ShowDialog();
            if (addfearform.DialogResult == DialogResult.Cancel || addfearform.DialogResult == DialogResult.Yes)
            {
                this.initPart();
            }
        }
    }
}
